package com.itaming.lycheeframework.mybatisflex.id;

import com.itaming.lycheeframework.support.id.SnowflakeIdGenerator;
import com.mybatisflex.core.keygen.IKeyGenerator;

import java.net.InetAddress;

/**
 * 自定义雪花算法ID生成器
 *
 * @author A.Ming
 */
public class LycheeSnowflakeIdGenerator implements IKeyGenerator {

    /**
     * 框架统一雪花算法ID生成器
     */
    private final SnowflakeIdGenerator generator;

    /**
     * 构造函数
     */
    public LycheeSnowflakeIdGenerator() {
        this.generator = new SnowflakeIdGenerator();
    }

    /**
     * 构造函数
     */
    public LycheeSnowflakeIdGenerator(InetAddress inetAddress) {
        this.generator = new SnowflakeIdGenerator(inetAddress);
    }

    /**
     * 构造函数
     */
    public LycheeSnowflakeIdGenerator(long workerId, long datacenterId) {
        this.generator = new SnowflakeIdGenerator(workerId, datacenterId);
    }

    /**
     * 生成ID
     *
     * @param entity    实体
     * @param keyColumn 主键字段
     * @return ID
     */
    @Override
    public Object generate(Object entity, String keyColumn) {
        return generator.nextId();
    }

}
